<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="com.example.dao.OrderDao" %>
<%@ page import="com.example.domain.Order" %>
<%@ page import="java.util.List" %>
<html>
<head>
    <title>用户信息 - 后台管理</title>
    <link rel="stylesheet" href="../assets/css/styles.css">
</head>
<body>
    <%
        // 检查用户是否已登录
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null) {
            response.sendRedirect("../auth/login.jsp");
            return;
        }
    %>
    
    <!-- 导航栏 -->
    <nav class="header-nav">
        <div class="nav-container">
            <a href="../index.jsp" class="nav-brand">购物网</a>
            
            <ul class="nav-links">
                <li><a href="../index.jsp">首页</a></li>
                <li><a href="../products/list.jsp">商品浏览</a></li>
                <li><a href="../cart/view/cart.jsp">购物车</a></li>
                <li><a href="user_info.jsp" class="active">用户中心</a></li>
            </ul>
            
            <div class="user-actions">
                <span class="user-info">欢迎, <%= currentUser.getUsername() %>!</span>
                <a href="../auth/logout.jsp" class="btn btn-outline">退出</a>
            </div>
        </div>
    </nav>
    
    <div class="container">
        <div class="page-title">
            <h1>用户信息</h1>
        </div>
        
        <div class="row">
            <div class="col-md-3">
                <div class="card">
                    <div class="user-info-sidebar">
                        <div class="user-avatar">
                            <div class="avatar-placeholder">用户头像</div>
                        </div>
                        <h3><%= currentUser.getUsername() %></h3>
                        <p>普通用户</p>
                        
                        <ul class="sidebar-menu">
                            <li class="active"><a href="user_info.jsp">个人信息</a></li>
                            <li><a href="#">修改密码</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="col-md-9">
                <div class="card">
                    <h3>基本信息</h3>
                    <div class="user-details">
                        <div class="form-group">
                            <label>用户名:</label>
                            <span><%= currentUser.getUsername() %></span>
                        </div>
                        <div class="form-group">
                            <label>邮箱:</label>
                            <span><%= currentUser.getEmail() != null ? currentUser.getEmail() : "未填写" %></span>
                        </div>
                        <div class="form-group">
                            <label>手机号:</label>
                            <span><%= currentUser.getPhone() != null ? currentUser.getPhone() : "未填写" %></span>
                        </div>
                    </div>
                </div>
                
                <div class="card">
                    <h3>我的订单</h3>
                    <%
                        try {
                            OrderDao orderDao = new OrderDao();
                            List<Order> orders = orderDao.getOrdersByUserId(currentUser.getId());
                            
                            if (orders.isEmpty()) {
                    %>
                    <p>暂无订单</p>
                    <%
                            } else {
                    %>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>订单号</th>
                                <th>总金额</th>
                                <th>状态</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <%
                                for (Order order : orders) {
                                    String statusText = "";
                                    switch (order.getStatus()) {
                                        case 0: statusText = "待支付"; break;
                                        case 1: statusText = "已支付"; break;
                                        case 2: statusText = "已发货"; break;
                                        case 3: statusText = "已完成"; break;
                                        case 4: statusText = "已取消"; break;
                                        default: statusText = "未知状态";
                                    }
                            %>
                            <tr>
                                <td><%= order.getOrder_no() != null ? order.getOrder_no() : "N/A" %></td>
                                <td>¥<%= String.format("%.2f", order.getTotal_amount()) %></td>
                                <td><%= statusText %></td>
                                <td><%= order.getCreated_at() != null ? order.getCreated_at().toString() : "N/A" %></td>
                                <td><a href="order_detail.jsp?id=<%= order.getId() %>">查看详情</a></td>
                            </tr>
                            <%
                                }
                            %>
                        </tbody>
                    </table>
                    <%
                            }
                        } catch (Exception e) {
                    %>
                    <p>加载订单信息失败: <%= e.getMessage() %></p>
                    <%
                        }
                    %>
                </div>
            </div>
        </div>
    </div>
</body>
</html>